package peer.gui;

import java.security.MessageDigest;
import java.util.Random;
import java.util.Scanner;

import common.Util;

public class PeerGUIDriver
{

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception
	{
		PeerGUI gui = PeerGUI.instance();
		UDPNetworkThread nwt = new UDPNetworkThread(gui);

		/*Scanner scan = new Scanner(System.in);
		boolean go = true;
		while(go)
		{
			System.out.print(": ");
			String line = scan.nextLine();
			if(line.startsWith("anc")) //addNetworkConnection
			{
				gui.addNetworkConnection(line.substring(line.indexOf(' ')), 10240);
			}
			else if(line.startsWith("rnc")) //removeNetworkConnection
			{
				boolean removed = gui.removeNetworkConnection(line.substring(line.indexOf(' ')), 10240);
				if(removed)
				{
					System.out.println("* element removed");
				}
				else
				{
					System.out.println("* element could not be removed");
				}
			}
			else if(line.startsWith("scs")) //setConnectionStatus
			{
				gui.setConnectionStatus(line.substring(line.indexOf(' ')));
			}
			else if(line.startsWith("afl")) //addFileToLibrary
			{
				//System.out.print("size: ");
				//long size = scan.nextLong();
				Random r = new Random(System.nanoTime());
				MessageDigest md = MessageDigest.getInstance("MD5");
				md.update(line.getBytes());
				long size = (long) Math.pow(2, r.nextInt(50));
				size *= r.nextDouble();
				gui.addFileToLibrary(line.substring(line.indexOf(' ')), size, Util.convertByteArrayHashcodeToHex(md.digest()));
				
			}
			else if(line.startsWith("ans")) //addNewSearch
			{
				gui.addNewSearch(line.substring(line.indexOf(' ')));
			}
			else if(line.startsWith("asr")) //addSearchResult
			{
				System.out.print("name: ");
				String filename = scan.nextLine();
				MessageDigest md = MessageDigest.getInstance("MD5");
				md.update(line.substring(line.indexOf(' ')).getBytes());
				Random r = new Random(System.nanoTime());
				long size = (long) Math.pow(2, r.nextInt(50));
				size *= r.nextDouble();
				gui.addSearchResult("and", line.substring(line.indexOf(' ')), size, Util.convertByteArrayHashcodeToHex(md.digest()));
			}
			else if(line.startsWith("l")) //log
			{
				gui.writeLog(line.substring(line.indexOf(' ')));
			}
			else if(line.startsWith("q")) //quit
			{
				System.exit(0);
			}
			else //invalid input
			{
				System.out.println("usage:\n    : a item  --adds an item\n    : r item  --removes an item\n    : l text  --writes text to log\n    : q  --quits");
			}
		}*/
	}

}
